//
//  UselessCouponViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/26.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "UselessCouponViewController.h"
#import "UselessCouponTableViewCell.h"
#import "MyCouponModel.h"
@interface UselessCouponViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UIImageView *_imageView;
    UILabel *_tipLbl;
}
@property (nonatomic,strong)UITableView * mainTableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,assign)NSInteger   reFreshindex;
@end

@implementation UselessCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.title=@"不可用优惠券";
    _dataSource=[NSMutableArray array];
    [self.view addSubview:self.mainTableView];
    [self.mainTableView.mj_header beginRefreshing];
    [self setUpBgView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}
#pragma mark--表
-(UITableView *)mainTableView
{
    if (!_mainTableView)
    {
        _mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64,kWidth, kHeight-64) style:UITableViewStylePlain];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTheTableView)];
        _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addTheMoreData)];
        [_mainTableView registerNib:[UINib nibWithNibName:@"UselessCouponTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        _mainTableView.backgroundColor = LIGHTGRAY;
        _mainTableView.tableFooterView = [UIView new];//设置当数据不多的时候，下面无cell
        _mainTableView.rowHeight=120;
        _mainTableView.sectionHeaderHeight = 0;
        _mainTableView.sectionFooterHeight = 0;
        _mainTableView.rowHeight=120;
    }
    return _mainTableView;
}
#pragma mark--刷新数据
-(void)refreshTheTableView
{
    _reFreshindex = 1;
    [_dataSource removeAllObjects];
    [_mainTableView reloadData];
    [self requestTheData];
}
-(void)addTheMoreData
{
    _reFreshindex++;
    [self requestTheData];
}
#pragma mark--请求数据
-(void)requestTheData
{
   // http://192.168.1.107:3722/User/MyCoupon/{UserCode}/{IsCanUse}/{PageIndex}/{PageSize}/{Timestamp}/{Sign}
    NSString *timestamp=[GeneralTool gettimestamp];
    
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,@"IsCanUse":@"0",@"PageIndex":[NSString stringWithFormat:@"%zd",_reFreshindex],@"PageSize":@"10",@"Timestamp":timestamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"User/MyCoupon/%@/%@/%@/%@/%@/%@",GETUSERCODE,@"0",[NSString stringWithFormat:@"%zd",_reFreshindex],@"10",timestamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        if ([responses[@"IsSuccess"]intValue]==1) {
            NSArray * dataArr = [responses objectForKey:@"ReturnObject"];
            if(dataArr.count==0)
            {
                //显示没有优惠卷
                _mainTableView.hidden=YES;
                _imageView.hidden=NO;
                _tipLbl.hidden=NO;
                return;
            }
            for (NSDictionary * couponDic in dataArr)
            {
                MyCouponModel * model = [[MyCouponModel alloc]init];
                [model setValuesForKeysWithDictionary:couponDic];
                [_dataSource addObject:model];
            }
            if (dataArr.count<15)
            {
                [_mainTableView.mj_footer endRefreshingWithNoMoreData];
            }else
            {
                [_mainTableView.mj_footer endRefreshing];
            }
        }else
        {
            [MBProgressHUD showError:@"获取信息失败"];
        }
        [_mainTableView reloadData];
        [_mainTableView.mj_header endRefreshing];
    }];
    
}
#pragma mark--创建没有拼单活动
-(void)setUpBgView
{
    _imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"no_coupon"]];
    _imageView.center=self.view.center;
    _imageView.hidden=YES;
    _imageView.bounds=CGRectMake(0, 5, 80, 80);
    [self.view addSubview:_imageView];
    
    _tipLbl=[[UILabel alloc]init];
    _tipLbl.center=CGPointMake(_imageView.center.x+40, _imageView.center.y+40+30);
    _tipLbl.bounds=CGRectMake(0, 0, 250, 30);
    _tipLbl.text=@"您还没有优惠券过期哦～";
    _tipLbl.textColor=GRAYCOLOR;
    _tipLbl.hidden=YES;
    [self.view addSubview:_tipLbl];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UselessCouponTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (indexPath.row > _dataSource.count-1)
    {
        return nil;
    }
    MyCouponModel * model = _dataSource[indexPath.row];
    cell.name.text = model.CategoryName;
    cell.category.text = model.CategoryNames;
    cell.city.text = model.CityNames;
    cell.title.text = model.CouponName;
    cell.time.text = [NSString stringWithFormat:@"有效期：%@",model.ShowDate];
    cell.price.text =[NSString stringWithFormat:@"%@",model.ShowMoney] ;
    cell.danWei.text = model.ShowSuffix;
    return cell;
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [GeneralTool showTabBar];
}

@end
